unit ResidualsChartUnit;

interface

uses DiagramFactory, datacontainer, pganalysistypesunit, apgabstractunit, classes,
   xmlintf, pgseriesunit, chartaxisunit, ifmmemoryunit, math;

type
   TResType = (rUnWeighted, rWeighted, rPhi, rWeight);
   ResidualsChart = class(DSChart)
   private
      m_ResType : TResType;
   public
      function CreateChartAxis(Axis : PgAxisType) : APgChartAxis; override;
      function CreateSeries(AOwner : TComponent; PrimaryChartAxis : APgChartAxis; MetaSeries : APgMetaSeries; XIndex : Integer) : APgSeries; override;
      constructor Create(AOwner : TComponent; datacontainer : TasacsDataContainer; _Ds : Integer; ResType : TResType); reintroduce;
      property ResType : TResType read m_ResType;
   end; // ResidualsChart


   ResidualSeries = class(PgSeries)
   protected
      function GetInternalValue(I : Integer; SeriesVariable : PgSeriesVariable; MetaSeries : ApgMetaSeries) : Double; override;
      function GetShowZeros() : Boolean; override;
   end; // ResidualSeries

implementation


//////////////////////////////////
// ResidualsChart


function ResidualsChart.CreateChartAxis(Axis : PgAxisType) : APgChartAxis;
begin
   case Axis of
      atx:
         Result := DsYearAxis.Create(Self as IChart, Axis);
      aty:
         Result := DsAgeAxis.Create(Self as IChart, Axis);
   else
      Result := PgChartAxis.Create(Self as IChart, Axis);
   end; // case
end; // ResidualsChart.CreateChartAxis


function ResidualsChart.CreateSeries(AOwner : TComponent; PrimaryChartAxis : APgChartAxis; MetaSeries : APgMetaSeries; XIndex : Integer) : APgSeries;
begin
   Result := nil;
   case PrimaryChartAxis.axis of
      atX:
         if not Assigned(MetaSeries) then
            Result := DsYearAxisSeries.Create(AOwner, PrimaryChartAxis, MetaSeries, XIndex);
      atY:
         if Assigned(MetaSeries) then
            Result := ResidualSeries.Create(AOwner, PrimaryChartAxis, MetaSeries, XIndex)
         else
            Result := DsAgeAxisSeries.Create(AOwner, PrimaryChartAxis, MetaSeries, XIndex)
   end; // case
end; // ResidualsChart.CreateSeries


constructor ResidualsChart.Create(AOwner : TComponent; datacontainer : TasacsDataContainer; _Ds : Integer; ResType : TResType);
var
   M : APgMetaSeries;
begin
   m_ResType := ResType;
   inherited Create(AOwner, datacontainer, _DS);
   case m_ResType of
      rUnWeighted: Caption := 'Unweighted';
      rWeighted: Caption := 'Weighted';
      rPhi: Caption := 'Phi';
      rWeight: Caption := 'Weight';
   end; // case
   case m_ResType of
      rUnWeighted: ChartLegend := 'ln(Obs/Mod)';
      rWeighted: ChartLegend := 'ln(Obs/Mod) * Weight';
      rPhi: ChartLegend := 'Phi';
      rWeight: ChartLegend := 'Weight';
   end; // case
//   ChartLegend := Caption;
   ShowChartLegend := true;
   ShowSeriesLegend := false;
   ShowZScaleLegend := true;
   ShowMeanLegend := false;
   Self.OrigoInCenterOfInterval := true;
//      ChartAxis[atX].ShowIntervalSeries := true;
   PlotType := ptCategoryMultiple;
   chartaxis[atX].ContinuousGroupData := false;
   chartaxis[atX].ShowMinorTick := true;
   chartaxis[atX].ShowMajorGrid := true;
   chartaxis[atY].ContinuousGroupData := false;
   chartaxis[atx].DefaultMin := true;
//   chartaxis[aty].MinValue := 0;
   //chartaxis[atX].AxisLabelPosition := alpCenter;
   chartaxis[aty].AxisLabel := 'Age';
   chartaxis[aty].AxisLabelPosition := alpCenter;
   chartaxis[atY].ShowMajorGrid := true;
   chartaxis[aty].DefaultAxisLabel := false;
   chartaxis[aty].DefaultMin := false;
   chartaxis[aty].MinValue := 0;
   ChartAxis[atZ].ZeroLine := false;
   ChartAxis[atZ].LnTransformed := false;

   ChartAxis[atZ].MinValue := -2;
   ChartAxis[atZ].DefaultMin := false;
   ChartAxis[atZ].MaxValue := 2;
   ChartAxis[atZ].DefaultMax := false;
   // Setup meta series at y axis:
   with ChartAxis[atZ].MetaSeries do
   begin
      M := TasacsMetaSeries.Create(ChartAxis[atz], nil, 'Residuals', 0);
      Add(Pointer(M));
      M.BarPlot := false;
      M.Scatter := true;
      M.Bubbles := true;
      M.ApplyInvert := true;
   end;
end; // ResidualsChart.Create


///////////////////////////////
// ResidualSeries


function ResidualSeries.GetInternalValue(I : Integer; SeriesVariable : PgSeriesVariable; MetaSeries : ApgMetaSeries) : Double;
begin
   case SeriesVariable of
      svIntervalDelta:
         with ResidualsChart(PrimaryChartAxis.ChartComp) do
            case ResType of
               rUnWeighted: Result := FloatMatrix(DataSourceContainer(Datacontainer.DS[DS]).DsData[ord(dsResU)])[PgChartAxis(ChartAxis[atX]).Series.MinCategory + XIndex, I];
               rWeighted: Result := FloatMatrix(DataSourceContainer(Datacontainer.DS[DS]).DsData[ord(dsResW)])[PgChartAxis(ChartAxis[atX]).Series.MinCategory + XIndex, I];
               rPhi: Result := FloatMatrix(DataSourceContainer(Datacontainer.DS[DS]).DsData[ord(dsPhi)])[PgChartAxis(ChartAxis[atX]).Series.MinCategory + XIndex, I];
               rWeight: Result := FloatMatrix(DataSourceContainer(Datacontainer.DS[DS]).DsData[ord(dsWeight)])[PgChartAxis(ChartAxis[atX]).Series.MinCategory + XIndex, I];
            end;
   else
      Result := inherited GetInternalValue(I, SeriesVariable, MetaSeries)
   end; // case
end; // ResidualSeries.GetInternalValue


function ResidualSeries.GetShowZeros() : Boolean;
begin
   Result := false;
end;


end.
